package com.blue.code.ftl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.blue.code.bean.EntityBean;
import com.blue.core.util.ReflectionUtil;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Component
public class TestHandler implements Handler
{
	private static Logger logger = LoggerFactory.getLogger(TestHandler.class);
	
	@Value("${ftl.test}")
	private String test;
	@Value("${ftl.facade}")
	private String facade;
	@Value("${ftl.exception}")
	private String exception;

	private Template template;
	
	public TestHandler()
	{
	}

	@Override
	public void handle(EntityBean bean, String output, String pkg) throws Exception
	{
		Map<String, Object> root = new HashMap<>();
		root.put("pkg", pkg + "." + test);
		root.put("exceptionPkg", pkg + "." + exception);
		root.put("messagePkg", pkg + "." + "message");
		root.put("facadePkg", pkg + "." + facade);
		root.put("clazz", bean.getClazz());
		root.put("name", bean.getName());
		String field = ReflectionUtil.clazzToField(bean.getClazz());
		root.put("field", field);
		root.put("columnList", bean.getColumnList());
		
		String clazz = bean.getClazz() + "ProviderTest";
		String fileName = this.output(template, root, output, test, clazz);
		logger.info("输出文件：{}", fileName);
	}

	@Override
	public void initConfiguration(Configuration conf, String ext) throws Exception
	{
		String ftl = test + ext;
		template = conf.getTemplate(ftl);
		logger.info("初始化 Test 模板：{}", ftl);
	}
}
